/******************************************************************************
 *
 * ngcap - network sniffer using raw sockets
 *
 * Author:        cr
 * E-mail:        cr.at.netgarage.org
 * WWW:           http://www.netgarage.org
 * Copyright (C): 2009 by cr
 *
 * This file is part of ngcap.
 *
 * ngcap is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ngcap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ngcap.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/

#ifndef _FRAME_HPP_
#define _FRAME_HPP_
#include "packet_header.hpp"
#include "trace.hpp"
#include "filter.hpp"


class Frame
{
public:
    Frame() {}
    virtual ~Frame() {}
    virtual std::string toString()=0;
    virtual Frame* getNext()=0;
    virtual bool matchFilter(PacketFilter* /*pf*/) { return false; } // correct? ***FIXME***
};

class PayloadFrame : public Frame
{
public:
    PayloadFrame(unsigned char* d, unsigned short s);
    std::string toString();
    unsigned short getSize() const;
    Frame* getNext();
private:
    unsigned char* data;
    unsigned short size;
    int minlength; //for filtering only strings longer then this value
};

class IcmpFrame : public Frame
{
public:
    IcmpFrame(unsigned char* data);
    std::string toString();
    Frame* getNext();
private:
    std::string getICMPType(unsigned char type);
    icmp_hdr* header;
};

class UdpFrame : public Frame
{
public:
    UdpFrame(unsigned char* d);
    std::string toString();
    Frame* getNext();
private:
    unsigned short payloadsize;
    udp_hdr* header;
    unsigned char* data;
};

class TcpFrame : public Frame
{
public:
    TcpFrame(unsigned char* d, unsigned short tot_len);
    std::string toString();
    unsigned short getSize() const;
    Frame* getNext();
    
private:
    tcp_hdr* header;
    unsigned short size;
    unsigned short payloadsize;
    unsigned char* data;
};

class ArpFrame : public Frame
{
public:
    ArpFrame(unsigned char* data);
    std::string toString();
    Frame* getNext();
private:
    std::string getOpCode(unsigned short int opcode);
    arp_packet* header;
};

class IpFrame : public Frame
{
public:
    IpFrame(unsigned char* d);
    std::string toString();
    unsigned short getSize() const;
    Frame* getNext();
private:
    ip_hdr* header;
    unsigned short protocol;
    unsigned short size;
    unsigned short packetsize;
    unsigned short version;
    unsigned char* data;
};

class EthernetFrame : public Frame
{
public:
    EthernetFrame(unsigned char* d);
    std::string toString();
    Frame* getNext();
    bool matchFilter(PacketFilter* pf);
    unsigned short getProtocol() const;
    eth_hdr* getHeader() const;
protected:
    eth_hdr* header;
    unsigned short protocol;
    unsigned char* data;
};

#endif
